package com.shushan.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.shushan.home.bean.RewardTimeBean;
import com.shushan.home.bean.TimeAwardBean;
import com.shushan.shushanbao.R;

import java.util.List;


/**
 * @ClassName: AwardToyAdapter
 * @Desciption: //时间奖励条目的适配器
 * @author: zhangshihao
 * @date: 2018-04-09
 */
public class AwardTimeListAdapter extends BaseAdapter{

    private List<RewardTimeBean> awardList;
    private Context mContext;
    private OnStartButtonClickListener2 listener;

    public AwardTimeListAdapter(Context context, List<RewardTimeBean> awardList) {
        this.mContext = context;
        this.awardList = awardList;
    }

    public void setOnStartButtonClickListener(OnStartButtonClickListener2 listener){
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return awardList.size();
    }

    @Override
    public Object getItem(int position) {
        return awardList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            //填充布局
            convertView =  LayoutInflater.from(mContext).inflate(R.layout.item_award_time, parent,false);
            holder = new ViewHolder();
            //通过ID查询控件
            holder.completeCondition = convertView.findViewById(R.id.tv_complete_condition);
            holder.completeExercising = convertView.findViewById(R.id.tv_complete_exercising);
            holder.btnStartExercise = convertView.findViewById(R.id.btn_start_exercise);
            holder.index = convertView.findViewById(R.id.tv_item_index);
            holder.viewHeadLine = convertView.findViewById(R.id.view_header_line);
            holder.viewBottomLine = convertView.findViewById(R.id.view_bottom_line);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(position == 0){
            holder.viewHeadLine.setVisibility(View.GONE);
        }else{
            holder.viewHeadLine.setVisibility(View.VISIBLE);
        }
        if(position == awardList.size() - 1){
            holder.viewBottomLine.setVisibility(View.GONE);
        }else{
            holder.viewBottomLine.setVisibility(View.VISIBLE);
        }
        RewardTimeBean bean = awardList.get(position);
        holder.index.setText(String.valueOf(position+1));
        holder.completeCondition.setText(bean.task);
        holder.completeExercising.setText(TimeAwardBean.getCompleteExercisingStr(bean.complete_num,bean.training_num));
        final int pos = position;
        final View v = convertView;
        if(listener != null){
            holder.btnStartExercise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onStartButtonClick2(v,pos);
                }
            });
        }

        return convertView;
    }

    public static class ViewHolder{
         TextView index;
         TextView completeCondition;
         TextView completeExercising;
         Button btnStartExercise;
         View viewHeadLine;
         View viewBottomLine;
    }

    public interface OnStartButtonClickListener2{
        void onStartButtonClick2(View itemView, int position);
    }

}
